#!/bin/bash
MYLOGFILE="/var/www/html/index.html"
source /usr/bin/gxcc_common.sh

#保证该脚本在同一时间只能有一个执行
if (set -o noclobber;echo "nothing" > ${WEB_DIR}/${LOCK_FILE_CHECK}) 2> /dev/null;then
    trap 'rm -f ${WEB_DIR}/${LOCK_FILE_CHECK};exit $?' INT TERM EXIT
    
    echo "<!DOCTYPE html><html>" > $MYLOGFILE
    echo "<p><a href=\"index.html\" target=\"_blank\"><b>本节点：${THIS_NODE_IP}</b></a>&emsp;" >> $MYLOGFILE
    echo "<a href=\"http://${ANOTHER_NODE_IP}/index.html\" target=\"_blank\">另一节点：${ANOTHER_NODE_IP}</a></p>" >> $MYLOGFILE
    echo "<p><b>脚本名称：${BASH_SOURCE[0]}</b></p>" >> $MYLOGFILE
    echo "<p><b>执行时间：$(date "+%Y-%m-%d %H:%M:%S")</b></p>" >> $MYLOGFILE
    echo "<p>链接：" >> $MYLOGFILE
    echo "<a href=\"stop.html\" target=\"_blank\">stop.html</a>&emsp;" >> $MYLOGFILE
    echo "<a href=\"start.html\" target=\"_blank\">start.html</a>&emsp;" >> $MYLOGFILE
    echo "<a href=\"backup.html\" target=\"_blank\">backup.html</a></p>" >> $MYLOGFILE

    check_httpd
    check_ntpd
    if [ `whoami` != "root" ];then echo "<p>须以root用户执行，执行结束</p></html>" >> $MYLOGFILE;exit 1;fi
    if ! check_this_node_ip $MYLOGFILE;then echo "<p>本节点IP设置错误，执行结束</p></html>" >> $MYLOGFILE;exit 1;fi
    
    check_this_node_running $MYLOGFILE
    if [ $? -eq 0 ];then
	echo "<p>本节点：RUNNING</p>" >> $MYLOGFILE
	echo "<p>STATE:RUNNING/$(date "+%Y-%m-%d %H:%M:%S")</p>" >> $MYLOGFILE
	check_another_node_running $MYLOGFILE $ANOTHER_NODE_IP
	if [ $? -eq 0 ]&&[ $THIS_NODE_IP -eq $BACKUP_NODE_IP ];then
	    echo "<p>两节点都为RUNNING，停掉BACKUP_NODE</p>" >> $MYLOGFILE
	    /usr/bin/gxcc_stop.sh > /dev/null
	    echo "<p>已执行gxcc_stop.sh</p>" >> $MYLOGFILE
	fi
	check_another_node_stopped $MYLOGFILE $ANOTHER_NODE_IP
	echo "<p>=====检查是否从${MESSAGE_SERVER}收到指令：开始=====</p>" >> $MYLOGFILE
	check_stop_message $MYLOGFILE
	if [ $? -eq 0 ];then
	    /usr/bin/gxcc_stop.sh > /dev/null
	    echo "<p>已执行gxcc_stop.sh</p>" >> $MYLOGFILE
	fi
	check_start_message $MYLOGFILE
	echo "<p>=====检查是否从${MESSAGE_SERVER}收到指令：结束=====</p>" >> $MYLOGFILE
    else
	echo "<p>本节点：不是RUNNING状态</p>" >> $MYLOGFILE
	check_this_node_stopped $MYLOGFILE
	if [ $? -eq 0 ];then
	    echo "<p>本节点：STOPPED</p>" >> $MYLOGFILE
	    echo "<p>STATE:STOPPED/$(date "+%Y-%m-%d %H:%M:%S")</p>" >> $MYLOGFILE
	    echo "<p>=====检查是否从${MESSAGE_SERVER}收到指令：开始=====</p>" >> $MYLOGFILE
	    check_stop_message $MYLOGFILE
	    check_start_message $MYLOGFILE
	    if [ $? -eq 0 ];then
		/usr/bin/gxcc_start.sh > /dev/null
		echo "<p>已执行gxcc_start.sh</p>" >> $MYLOGFILE
	    fi
	    echo "<p>=====检查是否从${MESSAGE_SERVER}收到指令：结束=====</p>" >> $MYLOGFILE
	else
	    echo "<p>本节点：异常</p>" >> $MYLOGFILE
	    /usr/bin/gxcc_stop.sh > /dev/null
	    echo "<p>已执行gxcc_stop.sh</p>" >> $MYLOGFILE
	    echo "<p>=====检查是否从${MESSAGE_SERVER}收到指令：开始=====</p>" >> $MYLOGFILE
	    check_stop_message $MYLOGFILE
	    check_start_message $MYLOGFILE
	    echo "<p>=====检查是否从${MESSAGE_SERVER}收到指令：结束=====</p>" >> $MYLOGFILE
	fi
    fi
    
    echo "<p>=====判断另一节点是否宕机：开始=====</p>" >> $MYLOGFILE
    check_connect_another_node $ANOTHER_NODE_IP
    if [ $? -eq 0 ];then
	echo "<p>另一节点：可以ping通</p>" >> $MYLOGFILE
    else
	echo "<p>另一节点：不能ping通</p>" >> $MYLOGFILE
	/usr/bin/gxcc_start.sh > /dev/null
	echo "<p>已执行gxcc_start.sh</p>" >> $MYLOGFILE
    fi
    echo "<p>=====判断另一节点是否宕机：结束=====</p>" >> $MYLOGFILE
    
    #echo '<iframe src="stop.html" width="100%" height="500" frameborder=1 scrolling="yes"></iframe>' >> /var/www/html/index.html
    echo "<p>执行结束</p></html>" >> $MYLOGFILE
else
    echo "重复执行"
fi
